package com.drama.api.company.bank.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.drama.api.base.Constants;
import com.drama.api.base.Type;
import com.drama.api.company.bank.dao.CompanyBankDao;
import com.drama.api.company.bank.domain.po.CompanyBankPO;
import com.drama.api.company.bank.domain.vo.CompanyBankAddVO;
import com.drama.api.company.bank.domain.vo.CompanyBankSearchVO;
import com.drama.api.company.bank.domain.vo.CompanyBankUpdateVO;
import com.drama.api.company.bank.domain.vo.CompanyBankVO;
import com.drama.api.company.bank.service.ICompanyBankService;
import com.drama.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
@Transactional
@Slf4j
public class CompanyBankServiceImpl implements ICompanyBankService {

    @Autowired
    private CompanyBankDao companyBankDao;

    @Override
    public List<CompanyBankVO> getList(CompanyBankSearchVO searchVO) {
        if(StringUtils.isBlank(searchVO.getCompanyId())){
            searchVO.setCompanyId((String) StpUtil.getLoginId());
        }
        return companyBankDao.getList(searchVO);
    }

    @Override
    public void add(CompanyBankAddVO vo) {
        CompanyBankPO po = new CompanyBankPO();
        BeanUtils.copyProperties(vo, po);
        po.preInsert();
        po.setBankState(2);
        po.setCompanyId( (String) StpUtil.getLoginId());
        int i = companyBankDao.insertSelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }

    }

    @Override
    public void delete(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        int i = companyBankDao.deleteByPrimaryKey(id);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public void update(CompanyBankUpdateVO updateVO) {
        CompanyBankPO po = new CompanyBankPO();
        BeanUtils.copyProperties(updateVO, po);
        po.preUpdate();
        int i = companyBankDao.updateByPrimaryKeySelective(po);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }

    @Override
    public CompanyBankVO get_default() {
        Example example=new Example(CompanyBankPO.class);
        example.and().andEqualTo("companyId",StpUtil.getLoginId()).andEqualTo("bankState",1);
        List<CompanyBankPO> list = companyBankDao.selectByExample(example);
        if (list.isEmpty()) {
            throw new BusinessException(Type.NOT_FOUND_ERROR.getCode(),  "没有绑定默认银行卡");
        }
        CompanyBankVO vo = new CompanyBankVO();
        BeanUtils.copyProperties(list.get(0), vo);
        return vo;
    }

    @Override
    public void set_default(String id) {
        if (StringUtils.isBlank(id)) {
            throw new BusinessException(Type.PARAM_VALIDATE_FAIL);
        }
        Example example=new Example(CompanyBankPO.class);
        example.and().andEqualTo("companyId",StpUtil.getLoginId()).andEqualTo("bankState",1);
        CompanyBankPO companyBankPO=new CompanyBankPO();
        companyBankPO.setBankState(2);
        companyBankDao.updateByExampleSelective(companyBankPO,example);
        companyBankPO.setId(id);
        companyBankPO.setBankState(1);
        companyBankPO.preUpdate();
        int i = companyBankDao.updateByPrimaryKeySelective(companyBankPO);
        if (i == Constants.SQL_RETURN_SUCCESS) {
            throw new BusinessException(Type.FAIL);
        }
    }
}